<?php

include_once '../lib/form_fields.class.php';

class forms {
	
	var $ff;
	var $out;
	var $langs;
	var $multilang = false;
	var $js_included = false;
	
	function forms() {
		$this->ff = new form_fields();
		if (is_object($GLOBALS['Lang'])) {
			$this->langs = $GLOBALS['Lang']->get_languages();
		} else {
			$this->langs = array('it');
		}
	}

	function open_form($name = 'form1', $action = '', $method = 'post', $multipart = false) {
		$enctype = ($multipart) ? ' enctype="multipart/form-data"' : '';
		$this->out .= '<form name="'.$name.'" action="'.$action.'" method="'. $method .'" '. $enctype .' autocomplete="off">';
	}
	
	function close_form($back_url = false) {
		$this->out .= '</form>';
	}
	
	function add_field($type, $name, $label, $hint, $default_value = '', $class = 'blockize') {
		if ($this->multilang) {
			foreach($this->langs as $lang) {
				$this->out .= $this->ff->$type($name, $label, $hint, $default_value, $class, $lang);
			}
		} else {
			$this->out .= $this->ff->$type($name, $label, $hint, $default_value, $class);
		}
	}
	
	// in futuro forse modificare per l'uso con i radio, i check, l'autocomplete e simili
	function add_select($name, $label, $hint, $values, $default_value = '', $class="blockize") {
		if ($this->multilang) {
			foreach($this->langs as $lang) {
				$this->out .= $this->ff->select($name, $label, $hint, $values, $default_value, $class, $lang);
			}
		} else {
			$this->out .= $this->ff->select($name, $label, $hint, $values, $default_value, $class);
		}
	}
	
	function add_submit($label = 'Salva', $back_url = false) {
		$this->out .= $this->ff->submit($label, $back_url);
	}
	
	function open_fieldset($legend = false, $multilang = false) {
		if ($multilang) {
			$this->multilang = true;
			$this->out .= $this->_multilang_selector();
		}
		$this->out .= '<fieldset>';
		if ($legend) {
			$this->out .= '<legend>'.$legend.'</legend>';
		}
	}
	
	function add_help($txt, $lang = false) {
		$this->out .= $this->ff->free_txt('<p>'.$txt.'</p>', $lang);
	}
	
	function _multilang_selector() {
		if (!$this->js_included) {
			$GLOBALS['Template']->set('includes', $this->_multilang_selector_js());
			$this->js_included = true;
		}
		$this->out .= '<div class="multilang_selector"><ul>';
		foreach($this->langs as $lang) {
			$this->out .= '<li><a href="javascript:;" class="'. $lang .'">'.$lang.'</a></li>';
		}
		$this->out .= '</ul></div>';
	}
	
	function _multilang_selector_js() {
		$out  = '<script language="javascript">'."\n";
		$out .= '<!--'."\n";
		$out .= '$(document).ready( function() {'."\n";
		$out .= '	$(\'form\').each( function () {'."\n";
		$out .= '		var frm = this;'."\n";
		$out .= '		$(\'fieldset div.lang\', frm).hide();'."\n";
		$out .= '		$(\'fieldset div.'. $this->langs[0] .'\', frm).show();'."\n";
		$out .= '		$(\'.multilang_selector a:first\', frm).addClass(\'selected\');'."\n";
		$out .= '		$(\'.multilang_selector a\', frm).click( function() {'."\n";
		$out .= '			var l = $(this).html();'."\n";
		$out .= '			$(\'fieldset div.lang\', frm).hide();'."\n";
		$out .= '			$(\'fieldset div.\'+l, frm).show();'."\n";
		$out .= '			$(\'.multilang_selector a\', frm).removeClass(\'selected\');'."\n";
		$out .= '			$(\'.multilang_selector a.\'+l, frm).addClass(\'selected\');'."\n";
		$out .= '		});'."\n";
		$out .= '	});'."\n";
		$out .= '});'."\n";
		$out .= '//-->'."\n";
		$out .= '</script>'."\n";
		return $out;
	}
	
	function close_fieldset() {
		$this->multilang = false;
		$this->out .= '</fieldset>';
	}
	
	function render() {
		$out = $this->out;
		$this->out = '';
		return $out;
	}

}

?>